package com.sgcc.ln.pmos.e_ln_home.activity;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sgcc.ln.pmos.arouter.home.HomeArouterPath;
import com.sgcc.ln.pmos.common.base.MvpBaseRecycleActivity;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.e_ln_home.adapter.UserTypeAdapter;
import com.sgcc.ln.pmos.e_ln_home.bean.UserTypeListBean;
import com.sgcc.ln.pmos.e_ln_home.contract.UserTypeListContract;
import com.sgcc.ln.pmos.e_ln_home.presenter.UserListPresenter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 用户类型时间轴
 */

@Route(path = HomeArouterPath.HOME_USERTYPENOW)
public class UserTypeNowActivity extends MvpBaseRecycleActivity<UserTypeListBean.DataDTO, UserListPresenter> implements UserTypeListContract.View {

    private UserTypeAdapter userTypeAdapter;
    int year = Calendar.getInstance().get(Calendar.YEAR);
    Calendar startDate = Calendar.getInstance();
    Calendar endDate = Calendar.getInstance();
    TimePickerView pvTime;
    List<UserTypeListBean.DataDTO> list = new ArrayList<>();

    @Override
    protected UserListPresenter createPresenter() {
        return new UserListPresenter();
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        userTypeAdapter = new UserTypeAdapter(mContext);
        userTypeAdapter.setOnItemClickListener(new UserTypeAdapter.OnItemClickListener() {
            @Override
            public void itemClickListener(int position, UserTypeListBean.DataDTO bean) {

            }
        });
        return userTypeAdapter;
    }

    @Override
    public void getDataRequest(int listSize, boolean isloading) {
        super.getDataRequest(listSize, isloading);

        mPresenter.getUserTypeList(year + "");
    }

//    @Override
//    public String getTitleText() {
//        return "身份转换记录";
//    }

    @Override
    protected void initViews() {
        super.initViews();
        year = Calendar.getInstance().get(Calendar.YEAR);
        title_bar.setTitle("多月身份信息");
        title_bar.setRightIconAndText(R.mipmap.down_jiantou, year + "年", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String time = title_bar.tvRight.getText().toString();
                if(!TextUtils.isEmpty(time)){
                    time.replace("年","");
                    SimpleDateFormat format = new SimpleDateFormat("yyyy");
                    try {
                        Date newTime = format.parse(time);
                        Calendar calendar1=    Calendar.getInstance();
                        calendar1.setTime(newTime);
                        pvTime.setDate(calendar1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                pvTime.show();
            }
        });
        initTimeSelect();

    }

    @Override
    public View getHeaderLayout(RelativeLayout parent) {
        View head_view = getLayoutInflater().inflate(R.layout.header_usettypenow, parent, false);
        return head_view;
    }

    @Override
    public void querySuccess(UserTypeListBean respon) {
        if (respon != null) {
            list.clear();
            String usertype = "";
            String endtime = "";
            if (respon.getData() != null) {
                if (!respon.getData().isEmpty()) {
                    for (int i = 0; i < respon.getData().size(); i++) {
                        if (!TextUtils.equals(usertype, respon.getData().get(i).getUserType())) {
                            usertype = respon.getData().get(i).getUserType();
                            if (list.size() != 0) {
                                list.get(list.size() - 1).setEndmonth(endtime);
                            }
                            list.add(respon.getData().get(i));
                        }
                        endtime = respon.getData().get(i).getMonth();
                        if (i == 11) {
                            list.get(list.size() - 1).setEndmonth(year + "-12");
                        }
                    }
                    loadSuccess(false, list);
                } else {
                    list.clear();
                    loadSuccess(false, list);
                }
            } else {
                list.clear();
                loadSuccess(false, list);
            }
        } else {
            ToastUtils.showShort("返回数据有误");
        }
    }

    public void setTime(String time) {
        year = Integer.valueOf(time);
        list.clear();
        refresh(false);
    }

    @Override
    public void queryFaile() {

    }

    private void initTimeSelect() {
        //正确设置方式 原因：注意事项有说明
        Calendar calendar = Calendar.getInstance();
        // 获取当前年
        int year = calendar.get(Calendar.YEAR);
        // 获取当前月
        int month = calendar.get(Calendar.MONTH) + 1;
        startDate.set(2021, month, 1);
        endDate.set(2099, 11, 1);
        //时间选择器
        pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                String time = TimeUtils.date2String(date, "yyyy");
                title_bar.tvRight.setText(time+"年");
                setTime(time);
            }
        }).setType(new boolean[]{true, false, false, false, false, false})
                .setDate(getDate())
                .setDecorView((ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content))
                .setRangDate(startDate, endDate)
                .build();
    }

    private Calendar getDate() {
        return Calendar.getInstance();
    }

}
